package com;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;

@SpringBootApplication
@MapperScan(basePackages = {"com.dao"})
public class SpringbootSchemaApplication extends SpringBootServletInitializer {

	public static void main(String[] args) {
		ConfigurableApplicationContext context = SpringApplication.run(SpringbootSchemaApplication.class, args);
		
		// 启动后自动打开浏览器访问页面
		try {
			// 等待应用完全启动
			Thread.sleep(2000);
			
			// 获取操作系统类型
			String os = System.getProperty("os.name").toLowerCase();
			String frontUrl = "http://localhost:8080/springboot482k3/front/index.html";
			String adminUrl = "http://localhost:8080/springboot482k3/admin/index.html";
			
			// 根据操作系统类型选择打开浏览器的方式
			if (os.contains("windows")) {
				Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + frontUrl);
				Thread.sleep(1000); // 等待一秒再打开第二个页面
				Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + adminUrl);
			} else if (os.contains("mac")) {
				Runtime.getRuntime().exec("open " + frontUrl);
				Thread.sleep(1000);
				Runtime.getRuntime().exec("open " + adminUrl);
			} else if (os.contains("linux")) {
				Runtime.getRuntime().exec("xdg-open " + frontUrl);
				Thread.sleep(1000);
				Runtime.getRuntime().exec("xdg-open " + adminUrl);
			}
			
			System.out.println("应用已启动，请访问:");
			System.out.println("前台页面: " + frontUrl);
			System.out.println("后台管理: " + adminUrl);
		} catch (Exception e) {
			System.out.println("应用已启动，请手动访问:");
			System.out.println("前台页面: http://localhost:8080/springboot482k3/front/index.html");
			System.out.println("后台管理: http://localhost:8080/springboot482k3/admin/index.html");
		}
	}
	
	@Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder applicationBuilder) {
        return applicationBuilder.sources(SpringbootSchemaApplication.class);
    }
}
